<!--  -->
<template>
<div>
    <a-modal v-model="modalVisible" :title="title" width="60%" @ok="save(dataSource)">
        <f-table ref="table" :dataSource="data" size="default"rowKey="busMaterialSrmSubId" :columns="columns"  :params="queryParam"  :multiple="false" :api="api.materialsms">
              <template v-for="col in editSlots" :slot="col" slot-scope="text, record, index" :row-selection="rowSelection">
                <div :key="col">
                  <a-input v-if="editingKey=== record.busMaterialSrmSubId&&col=='orderThisTimeQty'" style="margin: -5px 0" :value="text" @change="(e) => handleChange(e.target.value, record.busMaterialSrmSubId, col,record)" />
                    <template v-else>
                     {{ text }}
                   </template>
                </div>
              </template>
          <template slot="operation" slot-scope="text, record,index">
            <template >
              <a @click="() => edit(record,record.busMaterialSrmSubId)">编辑采购量</a>
            </template>
          </template>
        </f-table>
    </a-modal>
</div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import api from '../../material/app/js/api'
export default {
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
        //这里存放数据
        return {
           api: Object.assign(api, this.api),
            editingKey: '',
            modalVisible: false,
            load: false,
            title: '开口订单下达',
            //列表初始化查询参数
            queryParam: {},
             busMaterialSrmSubId: '',
            // 表头
            columns: [{
                    title: '合同编号',
                    dataIndex: 'contractNo'
                },
                {
                    title: '物资数量',
                    dataIndex: 'orderQty'
                },
                {
                    title: '已采购数量',
                    dataIndex: 'orderOutQty'
                },
                {
                    title: '本次采购数量',
                    dataIndex: 'orderThisTimeQty',
                    scopedSlots: {
                      customRender: 'orderThisTimeQty'
                    }

                },
               {
                title: '操作',
                align: 'center',
                scopedSlots: {
                  customRender: 'operation'
                }
              }

            ],
          editSlots: ['orderThisTimeQty'],
          dataSource: [],
          propTableData: {
            sel: null, // 选中行
            col: []
          }
        }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
        handleShow(sourceRecord) {
            this.queryParam.materialPlanId = sourceRecord.materialPlanId
            this.queryParam.ver = sourceRecord.ver
            this.modalVisible = true
           this.$refs.table.refresh()
        },
       edit(record, key) {
          debugger
          if (this.editingKey === key) {
              this.editingKey = ''
            } else {
              this.editingKey = key
            }
      },
      handleChange(value, key, column, record) {
        const newData = [...this.dataSource]
        const target = newData.filter((item) => key === item.busMaterialSrmSubId)[0]
        record[column] = value
        if (target != undefined && target) {
          target[column] = value
          this.dataSource = newData
        } else {
          const newData2 = {}
          newData2.busMaterialSrmSubId = key
          newData2[column] = value
          this.dataSource.push(newData2)
        }
      },
      save(dataSource) {
          debugger
        const jsonData = JSON.stringify(dataSource)
        this.http.post(this, {
          url: this.api.materialsms.upSrmPurseCount,
          data: {
            dataArray: jsonData
          },
          success: function (me, data) {
            me.$message.success('操作成功')
            me.$refs.table.refresh()
          }
        })
      }
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang="scss" scoped>
//@import url(); 引入公共css类
</style>
